package batchsvg;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Properties;

public class ImageDimensionValue extends TextualValue {

    File directory;
    boolean width;

    public ImageDimensionValue(String referencedKey, File directory, boolean width) {
        super(null, referencedKey);
        this.directory = directory;
        this.width = width;
    }

    @Override
    public String getValue(Properties valuesSoFar) {
        String value = super.getValue(valuesSoFar);
        if( value != null ) {
            try {
                BufferedImage image = Main.getImageForPath(directory, value);
                if( width ) {
                    value = Integer.toString(image.getWidth());
                } else {
                    value = Integer.toString(image.getHeight());
                }
            } catch( Exception ex ) {
                throw new RuntimeException(ex);
            }
        }
        return value;
    }
}
